package com.qo.qsdk.ui.base;

import android.app.Activity;
import android.content.res.Configuration;
import android.view.View;
import android.view.WindowManager;

import com.qo.qsdk.event.QSDKEvent;
import com.qo.qsdk.event.QSDKReceiver;
import com.qo.qsdk.event.QSDKSubscribe;
import com.qo.qsdk.model.HisModel;
import com.qo.qsdk.model.UserModel;
import com.qo.qsdk.ui.IAccount;
import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.QSDKHelper;
import com.qo.qsdk.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class AccountView implements IAccount {

    Activity mActivity;
    LoginView mLoginView;
    BindView mBindView;
    BindAView mBindAView;
    LoadingView mLoadingView;
    HisView mHisView;
    LoginInitView mLoginInitView;
    QSDKListener mIAccountListener;
    IBaseView mLastView;
    private List<IBaseView> mViews = new ArrayList<IBaseView>();

    public AccountView(Activity activity, QSDKListener iAccountListener) {
        mActivity = activity;
        mActivity.setContentView(DynamicUtils.layout(activity, "qsdk_main"));
        mIAccountListener = iAccountListener;
        mLoginView = new LoginView(mActivity, iAccountListener);
        mViews.add(mLoginView);
        mBindView = new BindView(mActivity, iAccountListener);
        mViews.add(mBindView);
        mLoadingView = new LoadingView(mActivity, iAccountListener);
        mViews.add(mLoadingView);
        mBindAView = new BindAView(mActivity, iAccountListener);
        mViews.add(mBindAView);
        mHisView = new HisView(mActivity, iAccountListener);
        mViews.add(mHisView);
        mLoginInitView = new LoginInitView(mActivity, iAccountListener);
        mViews.add(mLoginInitView);


        initView(activity.getResources().getConfiguration());
    }


    private void initView(Configuration configuration) {
        WindowManager.LayoutParams localLayoutParams;

        int screenWidth = ScreenUtil.widthPixels(mActivity);
        int factWidth = ScreenUtil.dip2px(mActivity, 432);
        int reservedWidth = (int) (screenWidth * 0.06f);

        if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            localLayoutParams = mActivity.getWindow().getAttributes();

            int w = factWidth;
//            int h = WindowManager.LayoutParams.WRAP_CONTENT;
//            int w = WindowManager.LayoutParams.WRAP_CONTENT;
            int h = WindowManager.LayoutParams.WRAP_CONTENT;

            if (localLayoutParams == null) {
                localLayoutParams = new WindowManager.LayoutParams(w, h);
            }

            localLayoutParams.width = w;
            localLayoutParams.height = h;

            mActivity.getWindow().setAttributes(localLayoutParams);
        } else if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            localLayoutParams = mActivity.getWindow().getAttributes();

            int w = 0;
            if (screenWidth - reservedWidth > factWidth) {
                w = factWidth;
            } else {
                w = screenWidth - reservedWidth;
            }
            int h = WindowManager.LayoutParams.WRAP_CONTENT;
//            int w = WindowManager.LayoutParams.WRAP_CONTENT;
//            int h = WindowManager.LayoutParams.WRAP_CONTENT;
            if (localLayoutParams == null) {
                localLayoutParams = new WindowManager.LayoutParams(w, h);
            } else {
                localLayoutParams.width = w;
                localLayoutParams.height = h;
            }
            mActivity.getWindow().setAttributes(localLayoutParams);
        }
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    private QSDKReceiver qsdkReceiver = new QSDKReceiver() {

        @QSDKSubscribe(event = QSDKEvent.CHANGE_LANGUAGE)
        void onChangeLanguage() {
            for (IBaseView iBaseView : mViews) {
                iBaseView.refresh();
            }
        }

    };

    @Override
    public void showLogin(Activity activity) {
        UserModel uinfo = QSDKHelper.getInstance().getUserInfoModel(activity);
        if (uinfo.getType() == UserModel.Type.Account.ordinal() || uinfo.getType() == UserModel.Type.Register.ordinal()) {
            showLogin(activity, uinfo.getAccount(), uinfo.getPass());
            return;
        }
        show(activity, mLoginView);
    }

    @Override
    public void showLoginInit(Activity activity) {
        show(activity, mLoginInitView);
    }

    @Override
    public void hideAll(Activity activity) {
        for (IBaseView v : mViews) {
            v.hide(activity);
        }
    }

    @Override
    public boolean isShowLoginInit(Activity activity) {
        return mLoginInitView.equals(mLastView);
    }

    @Override
    public void clearLoginPwd(Activity activity) {
        mLoginView.setPass("");
    }

    @Override
    public void showLogin(Activity activity, String account) {
        show(activity, mLoginView);
        mLoginView.setAccount(account);
    }

    @Override
    public void showLogin(Activity activity, String account, String pass) {
        show(activity, mLoginView);
        mLoginView.setAccount(account);
        mLoginView.setPass(pass);
    }

    @Override
    public void showHist(Activity activity, List<HisModel> list) {
        show(activity, mHisView);
        mHisView.addData(list);
    }

    @Override
    public void showRegister(Activity activity) {
        show(activity, mLoginView);
    }

    @Override
    public void showBind(Activity activity, UserModel userModel) {
        show(activity, mBindView);
        mBindView.setUserModel(userModel);
    }

    @Override
    public void showBindWithGuest(Activity activity, UserModel userModel) {
        show(activity, mBindView);
        mBindView.setUserModelWithGuest(userModel);
    }

    @Override
    public void showBindQ(Activity activity, UserModel u) {
        show(activity, mBindAView);
        mBindAView.setUserModel(u);
    }

    @Override
    public void showLoading(Activity activity) {
        show(activity, mLoadingView);
    }

    private void show(Activity activity, IBaseView p) {
        for (IBaseView v : mViews) {
            if (v == p) {
                v.show(activity);
                mLastView = v;
            }else{
                v.hide(activity);
            }
        }
    }
}
